package com.leyou.page.service.impl;

import com.leyou.common.utils.JsonUtils;
import com.leyou.item.client.ItemClient;
import com.leyou.item.dto.*;
import com.leyou.page.dto.SpecGroupVO;
import com.leyou.page.dto.SpecParamVO;
import com.leyou.page.service.GoodsPageService;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 虎哥
 */
@Service
public class GoodsPageServiceImpl implements GoodsPageService {

    private ItemClient itemClient;
    private StringRedisTemplate redisTemplate;

    public GoodsPageServiceImpl(ItemClient itemClient, StringRedisTemplate redisTemplate) {
        this.itemClient = itemClient;
        this.redisTemplate = redisTemplate;
    }

    private static final String KEY_PREFIX_SPU = "page:spu:id:";
    private static final String KEY_PREFIX_SKU = "page:sku:id:";
    private static final String KEY_PREFIX_DETAIL = "page:detail:id:";
    private static final String KEY_PREFIX_CATEGORY = "page:category:id:";
    private static final String KEY_PREFIX_BRAND = "page:brand:id:";
    private static final String KEY_PREFIX_SPEC = "page:spec:id:";

    @Override
    public String loadSpuData(Long spuId) {
        // 1.查询spu
        SpuDTO spuDTO = itemClient.querySpuById(spuId);
        // 2.封装并返回
        Map<String, Object> spu = new HashMap<>(4);
        spu.put("id", spuDTO.getId());
        spu.put("name", spuDTO.getName());
        spu.put("brandId", spuDTO.getBrandId());
        spu.put("categoryIds", spuDTO.getCategoryIds());
        // 3.序列化为json
        String json = JsonUtils.toJson(spu);
        // 4.写入redis
        redisTemplate.opsForValue().set(KEY_PREFIX_SPU + spuId, json);
        return json;
    }

    @Override
    public String loadSpuDetailData(Long spuId) {
        // 1.查询detail
        SpuDetailDTO spuDetailDTO = itemClient.querySpuDetailById(spuId);
        // 2.序列化为json
        String json = JsonUtils.toJson(spuDetailDTO);
        // 3.写入redis
        redisTemplate.opsForValue().set(KEY_PREFIX_DETAIL + spuId, json);
        return json;
    }

    @Override
    public String loadSkuListData(Long spuId) {
        // 1.查询sku
        List<SkuDTO> list = itemClient.querySkuBySpuId(spuId);
        // 2.序列化为json
        String json = JsonUtils.toJson(list);
        // 3.写入redis
        redisTemplate.opsForValue().set(KEY_PREFIX_SKU + spuId, json);
        return json;
    }

    @Override
    public String loadCategoriesData(List<Long> ids) {
        // 1.查询category
        List<CategoryDTO> categoryDTOList = itemClient.queryCategoryByIds(ids);
        // 2.封装并返回
        List<Map<String, Object>> list = new ArrayList<>(categoryDTOList.size());
        for (CategoryDTO categoryDTO : categoryDTOList) {
            Map<String, Object> map = new HashMap<>(2);
            map.put("id", categoryDTO.getId());
            map.put("name", categoryDTO.getName());
            list.add(map);
        }

        // 3.序列化为json
        String json = JsonUtils.toJson(list);
        // 4.写入redis
        redisTemplate.opsForValue().set(KEY_PREFIX_CATEGORY + ids.get(2), json);
        return json;
    }

    @Override
    public String loadBrandData(Long brandId) {
        // 1.查询brand
        BrandDTO brand = itemClient.queryBrandById(brandId);
        // 2.封装并返回
        Map<String, Object> map = new HashMap<>(2);
        map.put("id", brand.getId());
        map.put("name", brand.getName());

        // 3.序列化为json
        String json = JsonUtils.toJson(map);
        // 4.写入redis
        redisTemplate.opsForValue().set(KEY_PREFIX_BRAND + brandId, json);
        return json;
    }

    @Override
    public String loadSpecData(Long categoryId) {
        // 1.查询spec
        List<SpecGroupDTO> specGroupDTOList = itemClient.querySpecList(categoryId);
        // 2.封装并返回
        List<SpecGroupVO> groupVOList = new ArrayList<>(specGroupDTOList.size());
        // 2.1.遍历dto集合
        for (SpecGroupDTO groupDTO : specGroupDTOList) {
            // 2.2.创建vo对象
            SpecGroupVO groupVO = new SpecGroupVO();
            groupVOList.add(groupVO);
            // 2.3.拷贝dto的name属性到vo
            groupVO.setName(groupDTO.getName());
            // 2.4.把dtoList处理成voList
            List<SpecParamDTO> paramDTOList = groupDTO.getParams();
            List<SpecParamVO> paramVOList = new ArrayList<>();
            // 2.4.1.遍历paramDTOList
            for (SpecParamDTO paramDTO : paramDTOList) {
                // 2.4.2.创建paramVO
                SpecParamVO paramVO = paramDTO.toEntity(SpecParamVO.class);
                // 2.4.3.存
                paramVOList.add(paramVO);
            }
            groupVO.setParams(paramVOList);


        }

        // 3.序列化为json
        String json = JsonUtils.toJson(groupVOList);
        // 4.写入redis
        redisTemplate.opsForValue().set(KEY_PREFIX_SPEC + categoryId, json);
        return json;
    }

    @Override
    public Boolean deleteSku(Long spuId){
        return redisTemplate.delete(KEY_PREFIX_SKU + spuId);
    }
}
